﻿import "../Chart.js/chart.umd.min.js";

Chart.defaults.font.size = 16;

export function createChart(domCanvas,xLabel,yLabel) {
    if (domCanvas == null)
        return null;
    let chartConfig = {
        type: 'line',
        data: {
            labels: [0, 1],
            datasets: [{
                //label: 'line1',
                data: [NaN, NaN],
                borderWidth: 1,
                borderColor: '#8AA',
                backgroundColor: '#8AA',
                fill: false
            }]
        },
        options: {
            animation: false,
            maintainAspectRatio: false,
            interaction: {
                // Overrides the global setting
                mode: 'index',
                intersect: false
            },
            scales: {
                x: {
                    beginAtZero: true,
                    type: 'linear',
                    title: {
                        display: true,
                        text: xLabel
                    }
                //    min: 0,
                //    max: 360
                },
                y: {
                    beginAtZero: true,
                    type: 'linear',
                    title: {
                        display: true,
                        text: yLabel
                    }
                }
            },
            plugins: { legend: { display: false } }
        }
    };
    let chart = new Chart(domCanvas, chartConfig);
    domCanvas.chart = chart;
    //console.log(chart);
    return chart;
}

export function setChartData(domCanvas, xs, ys) {
    let chart = domCanvas?.chart;
    if (chart == null)
        return;
    //console.log('js.a');
    //console.log(xs);
    chart.data.labels = xs;
    chart.data.datasets[0].data = ys;
    //console.log('js.b');
    chart.update();
}
